#include <iostream>
using namespace std;

const int N=3e6+10;
int T, n, q, idex;
int a[N][70], p[N];

int get_char(char ch)
{
	if(ch<='z' && ch>='a') return ch-'a'+1;
	else if(ch<='Z' && ch>='A') return ch-'A'+27;
	else return ch-'0'+53;
}

void insert(string& s)
{
	int cur=0;
	p[cur]++;
	
	for(auto& ch: s)
	{
		int path=get_char(ch);
		if(a[cur][path]==0) a[cur][path]=++idex;
		cur=a[cur][path];
		p[cur]++;
	}
	
}

int flag(string& s)
{
	int cur=0;
	
	for(auto& ch: s)
	{
		int path=get_char(ch);
		if(a[cur][path]==0) return 0;
		cur=a[cur][path];
	}
	return p[cur];
}


int main()
{
	cin>>T;
	while(T--)
	{
		for(int i=0; i<=idex; i++)
		{
			p[i]=0;
			for(int j=1; j<70; j++)
				a[i][j]=0;
		}
		idex=0;
		cin>>n>>q;
		while(n--)
		{
			string s; cin>>s;
			insert(s);
		}
		while(q--)
		{
			string s; cin>>s;
			cout<<flag(s)<<endl;
		}
	}
	return 0;
 } 
